<?php

namespace wxml\controllers;

use wxml\models\order\Order;
use wxml\models\Weixin;
use Yii;
use common\libs\CommonController;
use common\libs\Util;

class OrderController extends CommonController
{
    public function actionGenerator()
    {
        $product_id = Yii::$app->request->post('product_id');
        $address = Yii::$app->request->post('address');

        $accessToken = yii::$app->request->post('access_token');
        $user = Weixin::checkIsLogin($accessToken);
        if ($user === false) {
            return $this->ajaxResponseError('用户还未登录，请重新登录', -999);
        }
        $order = new Order();
        $res = $order->createOrder($product_id, $address, $user);
        if ($res) {
            return $this->ajaxResponseSuccess($res);
        } else {
            return $this->ajaxResponseError($order->getErrMsg(), -1);
        }
    }

    public function actionDetail()
    {
        $order_id = Yii::$app->request->post('order_id');
        $accessToken = yii::$app->request->post('access_token');
        $user = Weixin::checkIsLogin($accessToken);
        if ($user === false) {
            return $this->ajaxResponseError('用户还未登录，请重新登录', -999);
        }
        $order = new Order();
        $result = $order->detail($order_id, $user);
        if ($result === false) {
            return $this->ajaxResponseError($order->getErrMsg(), -1);
        } else {
            return $this->ajaxResponseSuccess($result);
        }
    }

    public function actionList()
    {
        $accessToken = yii::$app->request->post('access_token');
        $user = Weixin::checkIsLogin($accessToken);
        if ($user === false) {
            return $this->ajaxResponseError('用户还未登录，请重新登录', -999);
        }
        $offset = yii::$app->request->post('offset');
        $limit = yii::$app->request->post('limit');

        $order = new Order();
        $result = $order->getList($user, $offset, $limit);
        if ($result === false) {
            return $this->ajaxResponseError($order->getErrMsg(), -1);
        } else {
            return $this->ajaxResponseSuccess($result);
        }
    }

    public function actionPay()
    {
        $order_id = Yii::$app->request->post('order_id');
        $accessToken = yii::$app->request->post('access_token');
        $user = Weixin::checkIsLogin($accessToken);
        if ($user === false) {
            return $this->ajaxResponseError('用户还未登录，请重新登录', -999);
        }

        $order = new Order();
        $result = $order->orderPay($order_id, $user);
        if ($result === false) {
            return $this->ajaxResponseError($order->getErrMsg(), -1);
        } else {
            return $this->ajaxResponseSuccess($result);
        }
    }

    public function actionPay_notify()
    {
        $return_code = yii::$app->request->getBodyParam('return_code');
        $return_msg = yii::$app->request->getBodyParams('return_msg');

        Yii::error('微信支付回调:'.print_r(yii::$app->request->getBodyParams(), true));
        $outPut = ['return_code' => 'FAILED', 'return_msg' => '签名失败'];
        if ($return_code === 'SUCCESS') {
            $params = yii::$app->request->getBodyParams();

            $paramsToSign = yii::$app->request->getBodyParams();
            unset($paramsToSign['sign']);
            Yii::error('微信支付回调:'.print_r($paramsToSign, true));

            $weixin = new Weixin();
            $mySign = $weixin->getSign($paramsToSign, 'md5', true, '&key=weixinlvacationcn1234567890abcde');
            if ($mySign === $params['sign'] && $params['result_code'] === 'SUCCESS') {
                $order = new Order();
                $res = $order->paySuccess($params['out_trade_no'], bcdiv($params['total_fee'], 100, 2));
                if ($res === false) {
                    $outPut = ['return_code' => 'FAILED', 'return_msg' => $order->getErrMsg()];
                } else {
                    $outPut = ['return_code' => 'SUCCESS', 'return_msg' => 'OK'];
                }
            } else {
                $outPut = ['return_code' => 'FAILED', 'return_msg' => '签名失败'];
            }
        }
        Yii::error('微信支付回调:'.print_r($outPut, true));

        return Util::arrayToXml($outPut);
    }
}
